package edu.mtu.citizenscience.ethnography;

import java.util.HashMap;
import java.util.Map;

import edu.mtu.citizenscience.ethnography.uploader.EthnographyUploadService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends FragmentActivity {

	private EditText usernameField;
	private EditText passwordField;
	private EditText confirmPasswordField;
	private EditText firstNameField;
	private EditText lastNameField;
	private EditText emailField;

	private BroadcastReceiver receiver = null;
	
	private Button registerButton;
	
	private LocalBroadcastManager manager;
	private IntentFilter intentFilter = new IntentFilter(EthnographyUploadService.UPLOAD_SERVICE);
	
	private static Map<String, Integer> loginStringIDs;

	static {
		loginStringIDs = new HashMap<String, Integer>();
		loginStringIDs.put(EthnographyUploadService.RESULT_SUCCESS, R.string.login_success);
		loginStringIDs.put(EthnographyUploadService.RESULT_FAILURE, R.string.login_failure);
		loginStringIDs.put(EthnographyUploadService.RESULT_ERROR, R.string.login_error);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_register);

		usernameField = (EditText) findViewById(R.id.username);
		passwordField = (EditText) findViewById(R.id.password);
		confirmPasswordField = (EditText) findViewById(R.id.confirmPassword);
		firstNameField = (EditText) findViewById(R.id.firstName);
		lastNameField = (EditText) findViewById(R.id.lastName);
		emailField = (EditText) findViewById(R.id.email);
		registerButton = (Button) findViewById(R.id.registerButton);
		registerButton.setEnabled(false);

		TextWatcher passwordListener = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				;
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				;
			}

			@Override
			public void afterTextChanged(Editable s) {
				registerButton.setEnabled(isValidRegister());
			}
		};
		
		usernameField.addTextChangedListener(passwordListener);
		passwordField.addTextChangedListener(passwordListener);
		confirmPasswordField.addTextChangedListener(passwordListener);
		firstNameField.addTextChangedListener(passwordListener);
		lastNameField.addTextChangedListener(passwordListener);
		emailField.addTextChangedListener(passwordListener);
		
		manager = LocalBroadcastManager.getInstance(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (receiver != null) {
			manager.registerReceiver(receiver, intentFilter);
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if (receiver != null) {
			manager.unregisterReceiver(receiver);
		}
	}

	private boolean isValidRegister() {
		boolean valid = true;

		// Check if passwords match
		String password = passwordField.getText().toString();
		String confirm = confirmPasswordField.getText().toString();
		if (password.equals(confirm)) {
			// Check if each field isn't empty (confirm is skipped because we already showed it = password)
			EditText[] allFields = {usernameField, passwordField, firstNameField, lastNameField, emailField};

			for (EditText field: allFields) {
				Editable text;

				text = field.getText();
				if (text.length() == 0) {
					valid = false;
					break;
				}
			}
		} else {
			valid = false;
		}

		return valid;
	}

	public void doRegister(View view) {
		if (isValidRegister()) {
			receiver = new LoginBroadcastReceiver();
			manager.registerReceiver(receiver, intentFilter);
			
			Intent intent = new Intent(this, EthnographyUploadService.class);
			intent.putExtra(EthnographyUploadService.INPUT_METHOD, EthnographyUploadService.METHOD_REGISTER);
			intent.putExtra(EthnographyUploadService.INPUT_USERNAME, usernameField.getText().toString());
			intent.putExtra(EthnographyUploadService.INPUT_PASSWORD, passwordField.getText().toString());
			intent.putExtra(EthnographyUploadService.INPUT_FIRST_NAME, firstNameField.getText().toString());
			intent.putExtra(EthnographyUploadService.INPUT_LAST_NAME, lastNameField.getText().toString());
			intent.putExtra(EthnographyUploadService.INPUT_EMAIL, emailField.getText().toString());
			
			startService(intent);
			
			registerButton.setEnabled(false);
		}
	}
	
	private class LoginBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String result = intent.getStringExtra(EthnographyUploadService.OUTPUT_RESULT);

			Toast toast = Toast.makeText(context, loginStringIDs.get(result), Toast.LENGTH_SHORT);
			toast.show();
			manager.unregisterReceiver(receiver);
			receiver = null;
		}

	}
}
